package com.wzw.springboot.principle.use.security.service;

import com.wzw.springboot.principle.use.security.dto.Register;
import com.wzw.springboot.principle.use.security.entity.UserEntity;
import com.wzw.springboot.principle.use.security.infrastructure.handle.PasswordEncoderHandler;
import com.wzw.springboot.principle.use.security.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author Wangzhiwen
 */
@Service
@RequiredArgsConstructor
public class UserDataServiceImpl implements UserDataService {

    private final UserRepository userRepository;

    @Override
    public Boolean registerUser(Register register) {
        UserEntity userEntity = new UserEntity().setUsername(register.getUsername())
                .setUid(UUID.randomUUID().toString());
        userEntity.setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        userEntity.setUsername(register.getUsername());
        String encode = PasswordEncoderHandler.encode(register.getPassword());
        userEntity.setPassword(encode);
        userEntity.setAccount("OP0000");
        userRepository.save(userEntity);
        return true;
    }

    @Override
    public UserEntity details(Long id) {
        return userRepository.findById(id).orElseThrow(RuntimeException::new);
    }
}
